﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness.Model;
using Bussiness.Model.Type;
using Bussiness.Service.Factory;
using Bussiness.Utilities;
using log4net;

namespace WEB.Admin.include {
    public partial class EditNew : UserControl {
        public EditNew() {
            IsFixed = false;
        }

        public bool IsFixed { get; set; }
        public long NewID { get; set; }
        public long CategoryId { get; set; }
        public long RooCategoryId { get; set; }

        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack) {
                InitControl();
            }
        }

        private void InitControl() {
            //load dropdow language
            Dl_Language.Items.Add(new ListItem("Tiếng Việt", Language.vi_VI.ToString()));
            Dl_Language.Items.Add(new ListItem("Tiếng Anh", Language.en_US.ToString()));
            if (IsFixed) {
                Dl_Language.SelectedIndexChanged += OnChangeLanguage;
            } else {
                if (RooCategoryId != 0) {
                    var list = CategoryServiceUtil.FindCategory(RooCategoryId).Children;
                    if (!list.IsEmpty) {
                        DL_Category.Items.Clear();
                        foreach (var category in list) {
                            DL_Category.Items.Add(new ListItem(category.Name, category.Id.ToString()));
                        }
                        Dl_Language.Visible = true;
                    }
                }
            }

            LoadNew();
        }

        private void LoadNew() {
            New _new = null;
            if (IsFixed) {
                if (CategoryId != 0) {
                    Language lang = DMUtil.String2Lang(Dl_Language.SelectedValue);
                    _new = NewServiceUtil.GetFirstResult(CategoryId, lang);
                }
            } else {
                if (NewID != 0) {
                    _new = NewServiceUtil.GetById(NewID);
                }
            }

            if (_new != null) {
                Txt_Title.Text = _new.Title;
                Txt_Description.Text = _new.Description;
                FCKeditorContent.Value = _new.ContentHtml;
                Img_Des.ImageUrl = DMUtil.ImagePath(_new.ImageDesUrl);
            } else {
                Img_Des.ImageUrl = DMUtil.NO_IMAGES_PATH;
            }
        }

        private void OnChangeLanguage(object sender, EventArgs e) {
            LoadNew();
        }

        protected void Btn_Save_Click(object sender, EventArgs e) {
        }

        protected void Btn_Cancel_Click(object sender, EventArgs e) {

        }

        protected void Btn_Upload_Click(object sender, EventArgs e) {
            
                string fileName = Ful_ImageDesciption.FileName;
                string path = DMUtil.USER_IMAGE_FILE_PATH;

                path += webapp4U.Common.GetFileName() + Path.GetExtension(fileName);
                webapp4U.EnumUpload uploadResult = webapp4U.FileProcess.UpLoadImage(Ful_ImageDesciption, path, 200);
                switch (uploadResult) {
                    case webapp4U.EnumUpload.UploadSucceed:
                        Img_Des.ImageUrl = path;
                        break;
                    case webapp4U.EnumUpload.FileNull:
                        DMUtil.Message(Resources.String.required_file);
                        break;
                    case webapp4U.EnumUpload.FileExtensionNotAgree:
                        DMUtil.Message(Resources.String.required_image_file);
                        break;
                    case webapp4U.EnumUpload.FileLarge:
                        DMUtil.Message(Resources.String.file_too_large_error);
                        break;
                    case webapp4U.EnumUpload.FileExits:
                        break;
                    default:
                        break;
                }
        }
    }

}
